home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / amiga / builtin2.zoo / arith.c next >
C/C++ Source or Header  |  1988-08-15  |  3KB  |  112 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24.  
  25. /*        some builtins for arithmetic functions            */
  26.  
  27. #include "builtin.h"
  28.  
  29. extern double floatval();
  30. extern word  makefloat();
  31. extern prettymuch_equal();
  32.     
  33. b_FLOATC()
  34. {        /* F, M, E, S : F is either a variable or a number (in WAM format);
  35.            M is either a number (in WAM format) or a variable; E is either
  36.            an integer or a variable.  The intended interpretation is that
  37.            M and E represent the mantissa and exponent, respectively, of
  38.            the floating point number F.  Either F, or both M and E, are
  39.            assumed to be bound (the selector S tells us which case it is),
  40.            and no checking for this is done here.            */
  41.  
  42.     register word op1, op2, op3, op4;
  43.     register pw top;
  44.     double fval; int exp0;
  45.  
  46.     op1 = gregc(1); deref(op1);
  47.     op2 = gregc(2); deref(op2);
  48.     op3 = gregc(3); deref(op3);
  49.     op4 = gregc(4); deref(op4);
  50.     switch ((int)intval(op4)) {
  51.     case 0 : { fval = frexp(numval(op1), &exp0);
  52.            follow(op2) = makefloat(fval); pushtrail(op2);
  53.            follow(op3) = makeint((word)exp0); pushtrail(op3);
  54.            break;
  55.          }
  56.     case 1 : { fval = ldexp( ((double)numval(op2)), (int)intval(op3) );
  57.            follow(op1) = makefloat(fval); pushtrail(op1);
  58.            break;
  59.          }
  60.     case 3 : { fval = ldexp( ((double)numval(op2)), (int)intval(op3) );
  61.            if (!prettymuch_equal(floatval(op1), fval))
  62.             {Fail0; break;}
  63.         }
  64.     };
  65.     return;
  66. }
  67.  
  68. b_ARITH()
  69. {
  70.     register word op1, op2, op3;
  71.     register pw top;
  72.     double y;
  73.  
  74.     op1 = gregc(1); 
  75.     op2 = gregc(2);
  76.     op3 = gregc(3); deref(op3);
  77.     switch ((int)intval(op3)) {
  78.     case 0 : {  deref(op2);
  79.             if (!unify(op1, makefloat(log((double)numval(op2)))))
  80.             {Fail0;}
  81.             break;
  82.          }
  83.     case 1 : {  deref(op1);
  84.             if (!unify(makefloat(exp((double)numval(op1))), op2))
  85.             {Fail0;}
  86.             break;
  87.          }
  88.     case 2 : {  deref(op2);
  89.             if (!unify(op1, makefloat(sqrt((double)numval(op2)))))
  90.             {Fail0;}
  91.             break;
  92.          }
  93.     case 3 : {  deref(op1);
  94.             y = (double)numval(op1);
  95.             if (!unify(makefloat(y * y), op2)) {Fail0;}
  96.             break;
  97.          }
  98.     case 4 : {  deref(op1);
  99.             if (!unify(makefloat(sin((double)numval(op1))), op2))
  100.             {Fail0;}
  101.             break;
  102.          }
  103.     case 5 : {  deref(op2);
  104.             if (!unify(op1, makefloat(asin((double)numval(op2)))))
  105.                 {Fail0;}
  106.             break;
  107.          }
  108.     };
  109.     return;
  110. }
  111.  
  112.